package com.soilflow.web.controller.soilflow;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mchange.lang.LongUtils;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.core.page.TableDataInfo;
import com.soilflow.common.domain.LandContract;
import com.soilflow.common.domain.LandPaymentPlan;
import com.soilflow.common.domain.LandRent;
import com.soilflow.common.domain.dto.LandPaymentPlanDto;
import com.soilflow.common.domain.vo.LandPaymentPlanVo;
import com.soilflow.info.service.ILandRentService;
import com.soilflow.info.service.LandContractService;
import com.soilflow.info.service.LandPaymentPlanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 新闻信息
 * @author chengshihua
 */
@RestController
@RequestMapping("/land/paymentPlan")
public class LandPaymentPlanController extends BaseController {

    @Autowired
    private LandPaymentPlanService landPaymentPlanService;
    @Autowired
    private ILandRentService landRentService;
    @Autowired
    private LandContractService landContractService;


    /**
     * 列表
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo landPaymentList(LandPaymentPlanDto dto) {
        startPage();
        List<LandPaymentPlanVo> list = landPaymentPlanService.getList(dto);
        //String beginTime = (String) dto.getParams().get("beginTime");
        //        //String endTime = (String) dto.getParams().get("endTime");
        //        //
        //        //QueryWrapper<LandPaymentPlan> queryWrapper = new QueryWrapper<>();
        //        //queryWrapper.lambda()
        //        //        .eq(!ObjectUtils.isEmpty(dto.getContractId()),LandPaymentPlan::getContractId,dto.getContractId())
        //        //        // 时间筛选
        //        //        .ge(!ObjectUtils.isEmpty(beginTime),LandPaymentPlan::getStartTime,beginTime)
        //        //        .le(!ObjectUtils.isEmpty(beginTime),LandPaymentPlan::getEndTime,endTime)
        //        //        // 通过创建人筛选
        //        //        .like(StringUtils.isNotEmpty(dto.getCreateBy()),LandPaymentPlan::getCreateBy,dto.getCreateBy());
        //        //
        //        //
        //        //List<LandPaymentPlan> list = landPaymentPlanService.list(queryWrapper);

        return getDataTable(list);
    }


    /**
     * 添加信息
     * @return
     */
    @PostMapping("/add")
    public AjaxResult addlandPaymentNotice(@RequestBody LandPaymentPlan landPaymentPlan){
        // 设置dept_id
        LandContract landContract = landContractService.getById(landPaymentPlan.getContractId());
        landPaymentPlan.setDeptId(landContract.getDeptId());

        Boolean result = landPaymentPlanService.save(landPaymentPlan);
        if (result){
            return AjaxResult.success();
        }
        return AjaxResult.error("请勿重复添加");
    }

    /**
     * 删除信息
     * @param ids
     * @return
     */
    @DeleteMapping("/del/{ids}")
    public AjaxResult dellandPayment(@PathVariable List<Long> ids){
        landPaymentPlanService.removeByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 更新信息
     * @return
     */
    @PutMapping("/update")
    public AjaxResult updatelandPayment(@RequestBody LandPaymentPlan paymentPlan){
        landPaymentPlanService.updateById(paymentPlan);
        return AjaxResult.success();
    }

    /**
     * 回显详情
     * @return
     */
    @GetMapping("/detail/{id}")
    public AjaxResult getlandPaymentById(@PathVariable Long id){
        LandPaymentPlan vo = landPaymentPlanService.getById(id);
        return AjaxResult.success(vo);
    }

    /**
     * 完成付款修改状态
     * @return
     */
    @GetMapping("/status/{id}")
    public AjaxResult editPaymentPlanStatus(@PathVariable Long id){
        LandPaymentPlan landPaymentPlan = landPaymentPlanService.getById(id);
        landPaymentPlan.setStatus("1");

        // 同时新增租金收取记录
        LandContract landContract = landContractService.getById(landPaymentPlan.getContractId());

        LandRent landRent = new LandRent();
        landRent.setLandId(landContract.getInfoId());
        landRent.setPrice(landPaymentPlan.getPrice());
        landRent.setStartTime(landPaymentPlan.getStartTime());
        landRent.setEndTime(new Date());
        landRent.setDeptId(landContract.getDeptId());
        landRentService.insertLandRent(landRent);
        return landPaymentPlanService.updateById(landPaymentPlan)?AjaxResult.success():AjaxResult.error();
    }
}
